package com.xc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.common.ServerResponse;
import com.xc.dao.AgentUserMapper;
import com.xc.dao.UserCashDetailMapper;
import com.xc.pojo.AgentUser;
import com.xc.pojo.User;
import com.xc.pojo.UserCashDetail;
import com.xc.service.IAgentUserService;
import com.xc.service.IUserCashDetailService;
import com.xc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/16 3:03 PM
 */
@Service("iUserCashDetailService")
public class UserCashDetailServiceImpl implements IUserCashDetailService {

    @Autowired
    UserCashDetailMapper userCashDetailMapper;
    @Autowired
    IUserService iUserService;
    @Autowired
    IAgentUserService iAgentUserService;
    @Autowired
    AgentUserMapper agentUserMapper;


    @Override
    public ServerResponse<PageInfo> findUserCashDetailList(Integer positionId, HttpServletRequest request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        User user = iUserService.getCurrentUser(request);

        List<UserCashDetail> userCashDetails = userCashDetailMapper.findUserCashDetailList(user.getId() , positionId);

        PageInfo pageInfo = new PageInfo(userCashDetails);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<PageInfo> listByAgent(Integer userId, String userName
            , Integer agentId, Integer positionId, HttpServletRequest request
            , int pageNum, int pageSize) {

        //  当前登陆代理
        AgentUser currentAgent = iAgentUserService.getCurrentAgent(request);
        //  如果传入了agentId,首先判断agentId是否为当前代理下
        if(agentId != null){
            AgentUser agentUser = agentUserMapper.selectByPrimaryKey(agentId);
            if(agentUser.getParentId() != currentAgent.getId()){
                return ServerResponse.createByErrorMsg("不能查询非下级代理用户持仓");
            }
        }
        Integer searchId = null;
        if(agentId == null){
            searchId = currentAgent.getId();    //  如果传入id为空，查询登陆代理的用户
        }else{
            searchId = agentId;
        }

        PageHelper.startPage(pageNum,pageSize);

        List<UserCashDetail> cashDetails = userCashDetailMapper
                .listByAgent(userId,userName,searchId,positionId);
        PageInfo pageInfo = new PageInfo(cashDetails);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<PageInfo> listByAdmin(Integer userId, String userName, Integer agentId
            , Integer positionId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<UserCashDetail> cashDetails = userCashDetailMapper
                .listByAdmin(userId,userName,agentId,positionId);
        PageInfo pageInfo = new PageInfo(cashDetails);
        return ServerResponse.createBySuccess(pageInfo);
    }


}
